<template>
  <el-dialog v-model="addOrEditVisible" title="海报配置" :before-close="closeEditDialog"
             width="50%">
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="160px" class="demo-ruleForm"
             :size="formSize" status-icon>
      <!--      <el-row>-->
      <!--        <el-col :span="24">-->
      <!--          <el-form-item label="海报ID" prop="id">-->
      <!--            <el-input v-model="ruleForm.id"/>-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--      </el-row>-->
      <!--      <el-row>-->
      <!--        <el-col :span="24">-->
      <!--          <el-form-item label="宽" prop="width">-->
      <!--            <el-input v-model="ruleForm.width"/>-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--      </el-row>-->
      <!--      <el-row>-->
      <!--        <el-col :span="24">-->
      <!--          <el-form-item label="高" prop="height">-->
      <!--            <el-input v-model="ruleForm.height"/>-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--      </el-row>-->
      <el-row>
        <el-col :span="24">
          <el-form-item label="海报名称" prop="posterName">
            <el-input v-model="ruleForm.posterName"/>
            <span class="form-tip">海报名称：便于用户查找相应的海报。</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="背景设置" prop="backgroundUrl">
            <form-up-load-image @uploadImageCallback="uploadImageCallback"
                                :imageUrl="ruleForm.backgroundUrl"></form-up-load-image>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="二维码前缀" prop="qrcodeUrl">
            <el-input v-model="ruleForm.qrcodeUrl"/>
            <span class="form-tip">设置普通链接二维码跳转小程序的规则。</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="标签" prop="posterTag">
            <el-select
                v-model="ruleForm.posterTags"
                multiple
                filterable
                allow-create
                default-first-option
                :reserve-keyword="false"
                style="width: 100%"
                placeholder="请输入标签">
              <!--              <el-option-->
              <!--                  v-for="item in options"-->
              <!--                  :key="item.value"-->
              <!--                  :label="item.label"-->
              <!--                  :value="item.value"-->
              <!--              />-->
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="海报描述" prop="posterDesc">
            <el-input v-model="ruleForm.posterDesc" type="textarea"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="ruleForm.remark" type="textarea"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
     <span class="dialog-footer">
       <el-button @click="closeEditDialog">关闭</el-button>
       <el-button type="primary" @click="submitForm(ruleFormRef)">提交</el-button>
     </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {reactive, ref} from 'vue'
import type {FormInstance, FormRules} from 'element-plus'
import {ElMessage} from "element-plus";
import {BookingSharePosterConfig} from '@/apis/model/booking/BookingSharePosterConfigModel';
import {
  insertBookingSharePosterConfig,
  updateBookingSharePosterConfig
} from '@/apis/http/booking/BookingSharePosterConfigHttp';
import FormUpLoadImage from "@/components/form/FormUpLoadImage.vue";
import {File} from "@/apis/model/system/FileModel";

interface Prop {
  addOrEditVisible: boolean,
  ruleForm: BookingSharePosterConfig,
  addDataType: string
}

// 刷新list页面
const refushList = ref(false);

// 传递过来的参数
const props = defineProps<Prop>();

const emit = defineEmits(["closeEditDialog"]);

// 关闭按钮
const closeEditDialog = () => {
  emit("closeEditDialog", {
    addOrEditVisible: !props.addOrEditVisible,
    refushList: refushList.value
  })
}
const formSize = ref('default')
const ruleFormRef = ref<FormInstance>()

const rules = reactive<FormRules>({
  width: [
    {required: true, message: '请输入宽', trigger: 'blur'}
  ],
  height: [
    {required: true, message: '请输入高', trigger: 'blur'}
  ],
  backgroundUrl: [
    {required: true, message: '请输入背景设置', trigger: 'blur'}
  ],
  // statusFlag: [
  //   {required: true, message: '请输入是否启用 0-否 1-是', trigger: 'blur'}
  // ],
  // remark: [
  //   {required: true, message: '请输入备注', trigger: 'blur'}
  // ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      // 新增
      if (props.addDataType === 'add') {
        insertBookingSharePosterConfig(props.ruleForm).then(res => {
          const code = res.code;
          const message: string = res.message;
          const data = res.data;
          if (code == 200) {
            ElMessage({
              message: '创建成功！',
              type: 'success',
            })
            refushList.value = true
            closeEditDialog()
          } else {
            ElMessage.error('创建失败！')
          }
        })
      } else if (props.addDataType === 'edit') {
        updateBookingSharePosterConfig(props.ruleForm).then(res => {
          const code = res.code;
          const message: string = res.message;
          const data = res.data;
          if (code == 200) {
            ElMessage({
              message: '更新成功！',
              type: 'success',
            })
            refushList.value = true
            closeEditDialog()
          } else {
            ElMessage.error('更新失败！')
          }
        })
      }
    } else {
      ElMessage.error('请完善信息！')
    }
  })
}

/**
 * 上传图片回调
 */
const uploadImageCallback = (e: File) => {
  props.ruleForm.backgroundUrl = e.url;
}
</script>

<style scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}

.form-tip {
  color: gray;
}
</style>
